
import requests
import json
from util.Config import *
from util.ErrorCode import *
from util.LoggerUtil import *
from util.TimeUtil import *

class UfidaUtil:
    def __init__(self, logger):
        self.logger = logger # LoggerUtil('util').logger

    #使用Get方式请求一个URL
    def HtmlGet(self, URL, params=None):
        #访问网页
        headers = {'Content-Type': 'application/json;charset=utf-8',
                'Authorization': Config['Api']['Token']}
        self.logger.debug("Http Get Request URL = " + URL + ", Parsms = " + str(params))
        result = requests.get(URL,  params=params, headers=headers);
        self.logger.debug("Http Get Response:" + result.text)
        return result;
        #end

    def HtmlPost(self, URL, data= None):
        headers = {'Content-Type': 'application/json;charset=utf-8',
                'Authorization': Config['Api']['Token']}
        self.logger.debug("Http Post Request URL = " + URL + ", data = " + str(data))
        result = requests.post(URL, json=data, headers=headers)
        self.logger.debug("Http Post Response:" + result.text)
        return result;


    def CheckChinese(self, string):
        for char in string:
            if '\u4e00' <= char <= '\u9fa5':
                return True
        return False

    def has_numbers(self, string):
        return any(char.isdigit() for char in string)

    def Error(self, msg):
        return {"code": Err, "msg": msg}
    def Success(self):
        return {"code": Suc, "msg": "success"}
    def Success1(self, data):
        return {"code": Suc, "msg": "success", "data": data}
    def Message(self, code, msg):
        return {"code": code, "msg": msg}

    
    def getEncoding(self, codeName, len, prefix):
        if not os.path.exists('codes'):
            os.makedirs('codes')
        idFn = 'codes/' + codeName + '_id.txt'
        try:
            with open(idFn, 'r') as file:
                id = int(file.read())
        except FileNotFoundError:
            id = 0
        id += 1
        with open(idFn, 'w') as file:
            file.write(str(id))
            # getYYYYMMDD() + 
        return prefix + str(id).zfill(len)
    

    # 读文件
    def readFileLines(self, filePath):
        # 打开文件
        lines = None
        with open(filePath, 'r') as file:
            # 读取所有行
            lines = file.readlines()
        # 关闭文件（在使用完文件后，建议始终关闭文件）
        file.close()
        return lines;

if __name__ == "__main__":
    # ApiLogin()
    # print(Config['Api']['Token']);
    # SkuList();
    print('util...')

    

